<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>The Front Controller - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.controller.front.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.controller.front.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.controller.basics.html">Zend_Controller Basics</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.controller.html">Zend_Controller</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.controller.request.html">The Request Object</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.controller.front" class="section"><div class="info"><h1 class="title">The Front Controller</h1></div>
    

    <div class="section" id="zend.controller.front.overview"><div class="info"><h1 class="title">Overview</h1></div>
        

        <p class="para">
            <span class="classname">Zend_Controller_Front</span> implements a <a href="http://www.martinfowler.com/eaaCatalog/frontController.html" class="link external">&raquo; Front
                Controller pattern</a> used in <a href="http://en.wikipedia.org/wiki/Model-view-controller" class="link external">&raquo; Model-View-Controller
                (MVC)</a> applications. Its purpose is to initialize the
            request environment, route the incoming request, and then dispatch
            any discovered actions; it aggregates any responses and returns them
            when the process is complete.
        </p>

        <p class="para">
            <span class="classname">Zend_Controller_Front</span> also implements the <a href="http://en.wikipedia.org/wiki/Singleton_pattern" class="link external">&raquo; Singleton
            pattern</a>, meaning only a single instance of it may be available at
            any given time. This allows it to also act as a registry on which
            the other objects in the dispatch process may draw.
        </p>

        <p class="para">
            <span class="classname">Zend_Controller_Front</span> registers a <a href="zend.controller.plugins.html" class="link">plugin broker</a> with
            itself, allowing various events it triggers to be observed by
            plugins. In most cases, this gives the developer the opportunity to
            tailor the dispatch process to the site without the need to extend
            the front controller to add functionality.
        </p>

        <p class="para">
            At a bare minimum, the front controller needs one or more paths to
            directories containing <a href="zend.controller.action.html" class="link">action
                controllers</a> in order to do its work. A variety of methods
            may also be invoked to further tailor the front controller
            environment and that of its helper classes.
        </p>

        <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Default Behaviour</b><br /></span>
            
            <p class="para">
                By default, the front controller loads the <a href="zend.controller.plugins.html#zend.controller.plugins.standard.errorhandler" class="link">ErrorHandler</a>
                plugin, as well as the <a href="zend.controller.actionhelpers.html#zend.controller.actionhelpers.viewrenderer" class="link">ViewRenderer</a>
                action helper plugin. These are to simplify error handling and
                view renderering in your controllers, respectively.
            </p>

            <p class="para">
                To disable the <em class="emphasis">ErrorHandler</em>, perform the following
                at any point prior to calling  <span class="methodname">dispatch()</span>:
            </p>

            <pre class="programlisting brush: php">
// Disable the ErrorHandler plugin:
$front-&gt;setParam(&#039;noErrorHandler&#039;, true);
</pre>


            <p class="para">
                To disable the <em class="emphasis">ViewRenderer</em>, do the following prior
                to calling  <span class="methodname">dispatch()</span>:
            </p>

            <pre class="programlisting brush: php">
// Disable the ViewRenderer helper:
$front-&gt;setParam(&#039;noViewRenderer&#039;, true);
</pre>

        </p></blockquote>
    </div>

    <div class="section" id="zend.controller.front.methods.primary"><div class="info"><h1 class="title">Primary Methods</h1></div>
        

        <p class="para">
            The front controller has several accessors for setting up its
            environment. However, there are three primary methods key to the
            front controller&#039;s functionality:
        </p>

        <div class="section" id="zend.controller.front.methods.primary.getinstance"><div class="info"><h1 class="title">getInstance()</h1></div>
            

            <p class="para">
                 <span class="methodname">getInstance()</span> is used to retrieve a front
                controller instance. As the front controller implements a
                Singleton pattern, this is also the only means possible for
                instantiating a front controller object.
            </p>

            <pre class="programlisting brush: php">
$front = Zend_Controller_Front::getInstance();
</pre>

        </div>

        <div class="section" id="zend.controller.front.methods.primary.setcontrollerdirectory"><div class="info"><h1 class="title">setControllerDirectory() and addControllerDirectory</h1></div>
            

            <p class="para">
                 <span class="methodname">setControllerDirectory()</span> is used to tell <a href="zend.controller.dispatcher.html" class="link">the dispatcher</a>
                where to look for <a href="zend.controller.action.html" class="link">action controller</a>
                class files. It accepts either a single path or an associative
                array of module and path pairs.
            </p>

            <p class="para">
                As some examples:
            </p>

            <pre class="programlisting brush: php">
// Set the default controller directory:
$front-&gt;setControllerDirectory(&#039;../application/controllers&#039;);

// Set several module directories at once:
$front-&gt;setControllerDirectory(array(
    &#039;default&#039; =&gt; &#039;../application/controllers&#039;,
    &#039;blog&#039;    =&gt; &#039;../modules/blog/controllers&#039;,
    &#039;news&#039;    =&gt; &#039;../modules/news/controllers&#039;,
));

// Add a &#039;foo&#039; module directory:
$front-&gt;addControllerDirectory(&#039;../modules/foo/controllers&#039;, &#039;foo&#039;);
</pre>


            <blockquote class="note"><p><b class="note">Note</b>: 
                <p class="para">
                    If you use  <span class="methodname">addControllerDirectory()</span> without a
                    module name, it will set the directory for the
                    <em class="emphasis">default</em> module -- overwriting it if it already
                    exists.
                </p>
            </p></blockquote>

            <p class="para">
                You can get the current settings for the controller directory
                using  <span class="methodname">getControllerDirectory()</span>; this will return an
                array of module and directory pairs.
            </p>
        </div>

        <div class="section" id="zend.controller.front.methods.primary.addmoduledirectory"><div class="info"><h1 class="title">addModuleDirectory() and getModuleDirectory()</h1></div>
            

            <p class="para">
                One aspect of the front controller is that you may <a href="zend.controller.modular.html" class="link">define a modular directory
                structure</a> for creating standalone components; these are
                called &quot;modules&quot;.
            </p>

            <p class="para">
                Each module should be in its own directory and mirror the
                directory structure of the default module -- i.e., it should
                have a <var class="filename">/controllers/</var> subdirectory at the minimum, and typically
                a <var class="filename">/views/</var> subdirectory and other application subdirectories.
            </p>

            <p class="para">
                 <span class="methodname">addModuleDirectory()</span> allows you to pass the name of
                a directory containing one or more module directories. It then
                scans it and adds them as controller directories to the front
                controller.
            </p>

            <p class="para">
                Later, if you want to determine the path to a particular module
                or the current module, you can call
                 <span class="methodname">getModuleDirectory()</span>, optionally passing a module
                name to get that specific module directory.
            </p>
        </div>

        <div class="section" id="zend.controller.front.methods.primary.dispatch"><div class="info"><h1 class="title">dispatch()</h1></div>
            

            <p class="para">
                 <span class="methodname">dispatch(Zend_Controller_Request_Abstract $request = null,
                    Zend_Controller_Response_Abstract $response = null)</span>
                does the heavy work of the front controller. It may optionally
                take a <a href="zend.controller.request.html" class="link">request
                    object</a> and/or a <a href="zend.controller.response.html" class="link">response object</a>,
                allowing the developer to pass in custom objects for each.
            </p>

            <p class="para">
                If no request or response object are passed in,
                 <span class="methodname">dispatch()</span> will check for previously registered
                objects and use those or instantiate default versions to use in
                its process (in both cases, the <acronym class="acronym">HTTP</acronym> flavor will be used as the
                default).
            </p>

            <p class="para">
                Similarly,  <span class="methodname">dispatch()</span> checks for registered <a href="zend.controller.router.html" class="link">router</a> and <a href="zend.controller.dispatcher.html" class="link">dispatcher</a>
                objects, instantiating the default versions of each if none is
                found.
            </p>

            <p class="para">
                The dispatch process has three distinct events:
            </p>

            <ul class="itemizedlist">
                <li class="listitem"><p class="para">Routing</p></li>
                <li class="listitem"><p class="para">Dispatching</p></li>
                <li class="listitem"><p class="para">Response</p></li>
            </ul>

            <p class="para">
                Routing takes place exactly once, using the values in the
                request object when  <span class="methodname">dispatch()</span> is called.
                Dispatching takes place in a loop; a request may either indicate
                multiple actions to dispatch, or the controller or a plugin may
                reset the request object to force additional actions to
                dispatch. When all is done, the front controller returns a
                response.
            </p>
        </div>

        <div class="section" id="zend.controller.front.methods.primary.run"><div class="info"><h1 class="title">run()</h1></div>
            

            <p class="para">
                 <span class="methodname">Zend_Controller_Front::run($path)</span> is a static
                method taking simply a path to a directory containing
                controllers. It fetches a front controller instance (via
                <a href="zend.controller.front.html#zend.controller.front.methods.primary.getinstance" class="link">getInstance()</a>,
                registers the path provided via <a href="zend.controller.front.html#zend.controller.front.methods.primary.setcontrollerdirectory" class="link">setControllerDirectory()</a>,
                and finally <a href="zend.controller.front.html#zend.controller.front.methods.primary.dispatch" class="link">dispatches</a>.
            </p>

            <p class="para">
                Basically,  <span class="methodname">run()</span> is a convenience method that can
                be used for site setups that do not require customization of the
                front controller environment.
            </p>

            <pre class="programlisting brush: php">
// Instantiate front controller, set controller directory, and dispatch in one
// easy step:
Zend_Controller_Front::run(&#039;../application/controllers&#039;);
</pre>

        </div>
    </div>

    <div class="section" id="zend.controller.front.methods.environment"><div class="info"><h1 class="title">Environmental Accessor Methods</h1></div>
        

        <p class="para">
            In addition to the methods listed above, there are a number of
            accessor methods that can be used to affect the front controller
            environment -- and thus the environment of the classes to which the
            front controller delegates.
        </p>

        <ul class="itemizedlist">
            <li class="listitem">
                <p class="para">
                     <span class="methodname">resetInstance()</span> can be used to clear all
                    current settings. Its primary purpose is for testing, but it
                    can also be used for instances where you wish to chain
                    together multiple front controllers.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">setDefaultControllerName()</span> and
                     <span class="methodname">getDefaultControllerName()</span> let you
                    specify a different name to use for the default controller
                    (&#039;index&#039; is used otherwise) and retrieve the current value.
                    They proxy to <a href="zend.controller.dispatcher.html" class="link">the dispatcher</a>.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">setDefaultAction()</span> and
                     <span class="methodname">getDefaultAction()</span> let you specify a
                    different name to use for the default action (&#039;index&#039; is
                    used otherwise) and retrieve the current value. They proxy
                    to <a href="zend.controller.dispatcher.html" class="link">the dispatcher</a>.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">setRequest()</span> and
                     <span class="methodname">getRequest()</span> let you specify <a href="zend.controller.request.html" class="link">the request</a>
                    class or object to use during the dispatch process and to
                    retrieve the current object. When setting the request
                    object, you may pass in a request class name, in which case
                    the method will load the class file and instantiate it.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">setRouter()</span>
                     <span class="methodname">getRouter()</span> let you specify <a href="zend.controller.router.html" class="link">the router</a>
                    class or object to use during the dispatch process and to
                    retrieve the current object. When setting the router
                    object, you may pass in a router class name, in which case
                    the method will load the class file and instantiate it.
                </p>

                <p class="para">
                    When retrieving the router object, it first checks to see if
                    one is present, and if not, instantiates the default router
                    (rewrite router).
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">setBaseUrl()</span> and
                     <span class="methodname">getBaseUrl()</span> let you specify <a href="zend.controller.request.html#zend.controller.request.http.baseurl" class="link">the base
                        <acronym class="acronym">URL</acronym></a> to strip when routing requests and to
                    retrieve the current value. The value is provided to the
                    request object just prior to routing.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">setDispatcher()</span> and
                     <span class="methodname">getDispatcher()</span> let you specify <a href="zend.controller.dispatcher.html" class="link">the
                        dispatcher</a> class or object to use during the
                    dispatch process and retrieve the current object. When
                    setting the dispatcher object, you may pass in a dispatcher
                    class name, in which case the method will load the class
                    file and instantiate it.
                </p>

                <p class="para">
                    When retrieving the dispatcher object, it first checks to see if
                    one is present, and if not, instantiates the default
                    dispatcher.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">setResponse()</span> and
                     <span class="methodname">getResponse()</span> let you specify <a href="zend.controller.response.html" class="link">the response</a>
                    class or object to use during the dispatch process and to
                    retrieve the current object. When setting the response
                    object, you may pass in a response class name, in which case
                    the method will load the class file and instantiate it.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">registerPlugin(Zend_Controller_Plugin_Abstract $plugin,
                        $stackIndex = null)</span> allows you to register <a href="zend.controller.plugins.html" class="link">plugin objects</a>.
                    By setting the optional <var class="varname">$stackIndex</var>, you can
                    control the order in which plugins will execute.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">unregisterPlugin($plugin)</span> let you
                    unregister <a href="zend.controller.plugins.html" class="link">plugin objects</a>.
                    <var class="varname">$plugin</var> may be either a plugin object or a
                    string denoting the class of plugin to unregister.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">throwExceptions($flag)</span> is used to turn on/off
                    the ability to throw exceptions during the dispatch process.
                    By default, exceptions are caught and placed in the <a href="zend.controller.response.html" class="link">response
                        object</a>; turning on  <span class="methodname">throwExceptions()</span>
                    will override this behaviour.
                </p>

                <p class="para">
                    For more information, read <a href="zend.controller.exceptions.html" class="link">MVC
                        Exceptions</a>.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">returnResponse($flag)</span> is used to tell the front
                    controller whether to return the response
                    (<b><tt>TRUE</tt></b>) from  <span class="methodname">dispatch()</span>, or if the
                    response should be automatically emitted
                    (<b><tt>FALSE</tt></b>). By default, the response is
                    automatically emitted (by calling
                     <span class="methodname">Zend_Controller_Response_Abstract::sendResponse()</span>);
                    turning on  <span class="methodname">returnResponse()</span> will override this
                    behaviour.
                </p>

                <p class="para">
                    Reasons to return the response include a desire to check for
                    exceptions prior to emitting the response, needing to log
                    various aspects of the response (such as headers), etc.
                </p>
            </li>
        </ul>
    </div>

    <div class="section" id="zend.controller.front.methods.params"><div class="info"><h1 class="title">Front Controller Parameters</h1></div>
        

        <p class="para">
            In the introduction, we indicated that the front controller also
            acts as a registry for the various controller components. It does so
            through a family of &quot;param&quot; methods. These methods allow you to
            register arbitrary data -- objects and variables -- with the front
            controller to be retrieved at any time in the dispatch chain. These
            values are passed on to the router, dispatcher, and action
            controllers. The methods include:
        </p>

        <ul class="itemizedlist">
            <li class="listitem">
                <p class="para">
                     <span class="methodname">setParam($name, $value)</span> allows you to set a
                    single parameter of <var class="varname">$name</var> with value
                    <var class="varname">$value</var>.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">setParams(array $params)</span> allows you to set
                    multiple parameters at once using an associative array.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">getParam($name)</span> allows you to retrieve a single
                    parameter at a time, using <var class="varname">$name</var> as the
                    identifier.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">getParams()</span> allows you to retrieve the entire
                    list of parameters at once.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">clearParams()</span> allows you to clear a single
                    parameter (by passing a string identifier), multiple named
                    parameters (by passing an array of string identifiers), or the
                    entire parameter stack (by passing nothing).
                </p>
            </li>
        </ul>

        <p class="para">
            There are several pre-defined parameters that may be set that have
            specific uses in the dispatch chain:
        </p>

        <ul class="itemizedlist">
            <li class="listitem">
                <p class="para">
                    <em class="emphasis">useDefaultControllerAlways</em> is used to hint to
                    <a href="zend.controller.dispatcher.html" class="link">the
                        dispatcher</a> to use the default controller in the
                    default module for any request that is not dispatchable
                    (i.e., the module, controller, and/or action do not exist).
                    By default, this is off.
                </p>

                <p class="para">
                    See <a href="zend.controller.exceptions.html#zend.controller.exceptions.internal" class="link">MVC Exceptions
                        You May Encounter</a>
                    for more detailed information on using this setting.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    <em class="emphasis">disableOutputBuffering</em> is used to hint to <a href="zend.controller.dispatcher.html" class="link">the
                        dispatcher</a> that it should not use output
                    buffering to capture output generated by action controllers.
                    By default, the dispatcher captures any output and appends
                    it to the response object body content.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    <em class="emphasis">noViewRenderer</em> is used to disable the <a href="zend.controller.actionhelpers.html#zend.controller.actionhelpers.viewrenderer" class="link">ViewRenderer</a>.
                    Set this parameter to <b><tt>TRUE</tt></b> to disable it.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    <em class="emphasis">noErrorHandler</em> is used to disable the <a href="zend.controller.plugins.html#zend.controller.plugins.standard.errorhandler" class="link">Error
                        Handler plugin</a>. Set this parameter to <b><tt>TRUE</tt></b> to
                    disable it.
                </p>
            </li>
        </ul>
    </div>

    <div class="section" id="zend.controller.front.subclassing"><div class="info"><h1 class="title">Extending the Front Controller</h1></div>
        

        <p class="para">
            To extend the Front Controller, at the very minimum you will need
            to override the  <span class="methodname">getInstance()</span> method:
        </p>

        <pre class="programlisting brush: php">
class My_Controller_Front extends Zend_Controller_Front
{
    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
}
</pre>


        <p class="para">
            Overriding the  <span class="methodname">getInstance()</span> method ensures that
            subsequent calls to
             <span class="methodname">Zend_Controller_Front::getInstance()</span> will return an
            instance of your new subclass instead of a
            <span class="classname">Zend_Controller_Front</span> instance -- this is particularly
            useful for some of the alternate routers and view helpers.
        </p>

        <p class="para">
            Typically, you will not need to subclass the front controller unless
            you need to add new functionality (for instance, a plugin
            autoloader, or a way to specify action helper paths). Some points
            where you may want to alter behaviour may include modifying how
            controller directories are stored, or what default router or
            dispatcher are used.
        </p>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.controller.basics.html">Zend_Controller Basics</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.controller.html">Zend_Controller</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.controller.request.html">The Request Object</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.controller.html">Zend_Controller</a></li>
  <li><a href="zend.controller.quickstart.html">Zend_Controller Quick Start</a></li>
  <li><a href="zend.controller.basics.html">Zend_Controller Basics</a></li>
  <li class="active"><a href="zend.controller.front.html">The Front Controller</a></li>
  <li><a href="zend.controller.request.html">The Request Object</a></li>
  <li><a href="zend.controller.router.html">The Standard Router</a></li>
  <li><a href="zend.controller.dispatcher.html">The Dispatcher</a></li>
  <li><a href="zend.controller.action.html">Action Controllers</a></li>
  <li><a href="zend.controller.actionhelpers.html">Action Helpers</a></li>
  <li><a href="zend.controller.response.html">The Response Object</a></li>
  <li><a href="zend.controller.plugins.html">Plugins</a></li>
  <li><a href="zend.controller.modular.html">Using a Conventional Modular Directory Structure</a></li>
  <li><a href="zend.controller.exceptions.html">MVC Exceptions</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>